package com.neusoft.ysu.populationmanagement.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.neusoft.ysu.populationmanagement.constant.TabelDic;
import com.neusoft.ysu.populationmanagement.entity.CommonResult;
import com.neusoft.ysu.populationmanagement.entity.ResPop;
import com.neusoft.ysu.populationmanagement.mapper.ResPopMapper;
import com.neusoft.ysu.populationmanagement.service.IResPopService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author 作者：赵子鉴
 * @since 2024-06-18
 */
@Service
public class ResPopServiceImpl implements IResPopService {

    @Autowired
    private ResPopMapper resPopMapper;

    @Override
    public CommonResult updateRP(ResPop resPop) {
        LambdaUpdateWrapper<ResPop> resPopLambdaUpdateWrapper = new LambdaUpdateWrapper<>();
        resPopLambdaUpdateWrapper.eq(ResPop::getPopId, resPop.getPopId());
        int rows = resPopMapper.update(resPop, resPopLambdaUpdateWrapper);
        if (rows <= 0) {
            return new CommonResult<>(0, "数据更新失败", null);
        }
        return new CommonResult<>(1, "常住人口表更新完成", null);
    }

    /**
     * @param popId
     * @return com.neusoft.ysu.populationmanagement.entity.CommonResult
     * @author 张幸晔
     * @date 2024-06-20 15:04
     */

    @Override
    public CommonResult selectRPBYPopId(Long popId) {
        LambdaQueryWrapper<ResPop> resPopLambdaQueryWrapper = new LambdaQueryWrapper<>();
        resPopLambdaQueryWrapper.eq(ResPop::getPopId, popId);
        List<ResPop> resPops = resPopMapper.selectList(resPopLambdaQueryWrapper);
        if (resPops.size() <= 0) {
            return new CommonResult<>(0, "根据popId检索失败", null);
        }
        return new CommonResult<>(1, "常住人口信息", resPops.get(0));

    }

    /**
     * @param resPop
     * @return
     * @author 张幸晔
     * @date 2024-06-20 15:20
     */
    @Override
    public CommonResult insertResPop(ResPop resPop) {
        resPop.setCreateTime(LocalDateTime.now());
        resPop.setIsDelete(TabelDic.IS_DELETE_NO);
        int rows = resPopMapper.insert(resPop);
        if (rows <= 0) {
            return new CommonResult<>(0, "插入常住人口错误", null);
        }
        return new CommonResult(1, "常住人口插入成功", null);
    }

    @Override
    public CommonResult deleteResPopByPopId(Long PopId) {
        List<ResPop> resPopList=resPopMapper.selectList(new LambdaQueryWrapper<ResPop>().eq(ResPop::getPopId,PopId).eq(ResPop::getIsDelete,0));
        if(resPopList.size()!=1) {
            return new CommonResult<>(0,"根据popId检索失败",null);
        }
        ResPop resPop=resPopList.get(0);
        resPop.setIsDelete(1);
        resPopMapper.update(resPop,new LambdaUpdateWrapper<ResPop>().eq(ResPop::getId,resPop.getId()));

        return new CommonResult<>(1,"删除成功",null);
    }

    @Override
    public CommonResult selectRPByHouseNum(String houseNum) {
        LambdaQueryWrapper<ResPop> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(ResPop::getHousenum,houseNum);
        List<ResPop> resPops = resPopMapper.selectList(lambdaQueryWrapper);
        if (resPops.size()<=0){
            return new CommonResult(0,"本户常住人口检索失败",null);
        }
        return new CommonResult<>(1,"本户常住人口列表",resPops);
    }


}
